'''
Created on Nov 11, 2011

@author: Victor Bos
'''

import Action

######################################################################
class UndoRedoStack:

    def __init__(self):
        self.clear()
        self.unsaved_changes = False

    def __copy(self):
        urs = UndoRedoStack()
        urs.undo_stack = self.undo_stack[:]
        urs.redo_stack = self.redo_stack[:]
            
    def has_unsaved_changes(self):
        return self.unsaved_changes

    def set_unsaved_changes(self, b):
        self.unsaved_changes = b
        
    def clear(self):
        self.undo_stack = []
        self.redo_stack = []
        
    def undo(self, root=None):
        if len(self.undo_stack) > 0:
            action = self.undo_stack[-1]
            del self.undo_stack[-1]
            self.redo_stack.append(action)
            action.undo(root)
            self.set_unsaved_changes(True)

    
    def redo(self, root=None):
        if len(self.redo_stack) > 0:
            action = self.redo_stack[-1]
            del self.redo_stack[-1]
            self.undo_stack.append(action)
            action.redo(root)
            self.set_unsaved_changes(True)

    def undo_top(self):
        return self.undo_stack[-1]
    
    def redo_top(self):
        return self.redo_stack[-1]
    
    def push(self, a):
        self.undo_stack.append(a)
        self.redo_stack = []

    def push_action(self, action, root):
        self.push(action)
        action.redo(root)
        self.set_unsaved_changes(True)
        

    def can_undo(self):
        return len(self.undo_stack) > 0
    
    def can_redo(self):
        return len(self.redo_stack) > 0
    
    def is_empty(self):
        return self.can_undo() or self.can_redo()
    
    